import { defineStore } from 'pinia'
import {
  getPlanList,
  savePlan,
  deletePlan,
  getPlanDetail,
  getScheduleList,
  saveSchedule,
  deleteSchedule,
  getScheduleDetail,
  getWorkList,
  saveWork,
  deleteWork,
  getWorkDetail,
} from '../services/oa'

interface ApiResponse<T> {
  code: number
  msg: string
  data: T
  count: number
}

interface Plan {
  id: number
  title: string
  content: string
  type: number
  remind_type: number
  remind_time: string
  start_time: string
  end_time: string
  create_time: string
}

export interface Schedule {
  id: number
  title: string
  content: string
  labor_type: number
  labor_time: number
  start_time: string
  end_time: string
  create_time: string
}

export interface Work {
  id: number
  types: number
  works: string
  file_ids: string
  start_date: string
  end_date: string
  create_time: string
}

interface OAState {
  plans: Plan[]
  planTotal: number
  schedules: Schedule[]
  scheduleTotal: number
  works: Work[]
  workTotal: number
}

export const useOAStore = defineStore('oa', {
  state: (): OAState => ({
    plans: [],
    planTotal: 0,
    schedules: [],
    scheduleTotal: 0,
    works: [],
    workTotal: 0,
  }),

  actions: {
    // 获取计划列表
    async getPlanListAction(params: any) {
      try {
        const res = await getPlanList(params)
        if (res.code === 0) {
          this.plans = res.data
          this.planTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 保存计划
    async savePlanAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await savePlan(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除计划
    async deletePlanAction(id: number): Promise<ApiResponse<any>> {
      try {
        const res = await deletePlan(id)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取计划详情
    async getPlanDetailAction(id: number): Promise<ApiResponse<any>> {
      try {
        const res = await getPlanDetail(id)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取日程列表
    async getScheduleListAction(params: any) {
      try {
        const res = await getScheduleList(params)
        if (res.code === 0) {
          this.schedules = res.data
          this.scheduleTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 保存日程
    async saveScheduleAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await saveSchedule(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除日程
    async deleteScheduleAction(id: number): Promise<ApiResponse<any>> {
      try {
        const res = await deleteSchedule(id)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取日程详情
    async getScheduleDetailAction(id: number): Promise<ApiResponse<any>> {
      try {
        const res = await getScheduleDetail(id)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取工作汇报列表
    async getWorkListAction(params: any): Promise<ApiResponse<any[]>> {
      try {
        const res = await getWorkList(params)
        if (res.code === 0) {
          this.works = res.data
          this.workTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 保存工作汇报
    async saveWorkAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await saveWork(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除工作汇报
    async deleteWorkAction(id: number): Promise<ApiResponse<any>> {
      try {
        const res = await deleteWork(id)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取工作汇报详情
    async getWorkDetailAction(params: any): Promise<ApiResponse<any>> {
      try {
        const res = await getWorkDetail(params)
        return res
      } catch (error) {
        throw error
      }
    },
  },
})
